<template>
  <div class="goods">
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>
        <a href="/hello">首页</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 搜索栏 -->
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            @keyup.enter.native="getGoodsListInfo"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsListInfo"
          >
            <el-button slot="append" @click="getGoodsListInfo" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品 -->
        <el-col :span="17">
          <el-button type="primary" @click="$router.push('/add_Goods')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsInfoList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_number" width="100px"></el-table-column>
        <el-table-column label="商品数量" prop="goods_weight" width="100px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="170px"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="editRowList(scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              @click="removeGoods(scope.row)"
              type="danger"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 编辑商品信息 -->

    <el-dialog
      @close="closeEditGoodsRules"
      title="编辑商品"
      :visible.sync="dialogVisibleGoods"
      width="50%"
    >
      <el-form :model="editFormInfo" :rules="editGoodsRules" label-width="100px" ref="editGoodsRef">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editFormInfo.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input type="Number" v-model="editFormInfo.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input type="Number" v-model="editFormInfo.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input type="Number" v-model="editFormInfo.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleGoods = false">取 消</el-button>
        <el-button type="primary" @click="confimEditGoodsInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { GetGoodsList, ConfirmEditInfo, RemoveGoods } from '@/api/api.js'
import moment from 'moment'

export default {
  name: 'BackShopGoods',

  data () {
    return {
      // 请求参数
      queryInfo: {
        // 搜索值
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      // 商品列表
      goodsInfoList: [],
      // 编辑商品show
      dialogVisibleGoods: false,
      // 编辑商品信息校验
      editGoodsRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入商品价钱/必须为数字', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请输入商品数量/必须为数字', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量/必须为数字', trigger: 'blur' },
        ],
      },
      // 编辑信息的数据
      editFormInfo: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
      }
    };
  },

  created () {
    this.getGoodsList()
  },

  methods: {
    // 获取商品信息
    async getGoodsList () {
      const { data: res } = await GetGoodsList(this.queryInfo)
      if (res.meta.status !== 200) return this.$message.error('请求商品失败')
      res.data.goods.forEach(item => {
        item.add_time = moment(item.add_time).format('YYYY-MM-DD HH:MM:SS')
      })
      this.goodsInfoList = res.data.goods
      this.total = res.data.total
      this.$message.success('请求商品成功')
    },
    // 查询参数
    getGoodsListInfo () {
      // 判断输入的值是否为空
      if (this.queryInfo.query.trim().length === 0) return this.$message.info('输入为空')
      this.getGoodsList()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList()
    },
    // 编辑商品信息
    editRowList (row) {
      this.dialogVisibleGoods = true
      // 赋值编辑表单
      console.log(row);
      this.editFormInfo = row
    },
    // 确认编辑信息提交
    confimEditGoodsInfo () {
      this.$refs.editGoodsRef.validate(async validate => {
        if (!validate) return
        const { data: res } = await ConfirmEditInfo(this.editFormInfo.goods_id, this.editFormInfo)
        if (res.meta.status !== 201) return this.$message.error('编辑失败')
      })
    },
    // 清除编辑表单校验
    closeEditGoodsRules () {
      this.$refs.editGoodsRef.resetFields()
    },
    // 删除商品
    async removeGoods (row) {
      const relust = await this.$messageBox('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err);
      if (relust !== 'confirm') return this.$message.info('取消删除')
      console.log();
      const { data: res } = await RemoveGoods(row.goods_id)
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error('删除失败！')
      this.$message.success('删除成功');
      this.getGoodsList()
    }
  },
};
</script>

<style lang="less" scoped>
</style>